package com.habitracker.view;

import com.habitracker.controller.DataManager;
import com.habitracker.model.Habit;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.*;

public class HabitPage {
    private DataManager dataManager;
    private VBox mainLayout;

    public HabitPage(DataManager dataManager) {
        this.dataManager = dataManager;
        this.mainLayout = createMainLayout();
    }

    public VBox getLayout() { return mainLayout; }

    private VBox createMainLayout() {
        VBox mainLayout = new VBox(15);
        mainLayout.setPadding(new Insets(15));
        mainLayout.setPrefSize(800, 600);
        mainLayout.setStyle("-fx-background-color: #ffffff;");

        // 标题
        Label title = new Label("习惯列表");
        title.setStyle("-fx-font-size: 24px; -fx-font-weight: bold; -fx-text-fill: #333333;");

        // 习惯列表容器 - 带滚动条
        ScrollPane scrollPane = createHabitsScrollPane();

        // 添加习惯区域
        HBox addHabitBox = createAddHabitBox();

        mainLayout.getChildren().addAll(title, scrollPane, addHabitBox);
        return mainLayout;
    }

    private ScrollPane createHabitsScrollPane() {
        VBox habitsContainer = new VBox(10);
        habitsContainer.setPadding(new Insets(10));

        for (Habit habit : dataManager.getHabits()) {
            VBox habitCard = createHabitCard(habit);
            habitsContainer.getChildren().add(habitCard);
        }

        ScrollPane scrollPane = new ScrollPane(habitsContainer);
        scrollPane.setFitToWidth(true);
        scrollPane.setPrefViewportHeight(450); // 固定高度
        scrollPane.setStyle("-fx-background: transparent; -fx-border-color: #e0e0e0;");

        return scrollPane;
    }

    private VBox createHabitCard(Habit habit) {
        VBox card = new VBox(8);
        card.setPadding(new Insets(12));
        card.setStyle("-fx-background-color: #f8f9fa; -fx-border-color: #e0e0e0; -fx-border-radius: 8; " +
                "-fx-background-radius: 8;");
        card.setPrefWidth(750); // 适应800宽度

        // 习惯名称
        Label nameLabel = new Label(habit.getName());
        nameLabel.setStyle("-fx-font-size: 18px; -fx-font-weight: bold; -fx-text-fill: #333333;");

        // 习惯信息
        Label createDateLabel = new Label("创建日期：" + habit.getFormattedCreateDate());
        createDateLabel.setStyle("-fx-font-size: 14px; -fx-text-fill: #666666;");

        Label totalDaysLabel = new Label("累计打卡天数：" + habit.getTotalCheckInDays());
        totalDaysLabel.setStyle("-fx-font-size: 14px; -fx-text-fill: #666666;");

        Label maxDaysLabel = new Label("最长连续打卡天数：" + habit.getMaxContinuousDays());
        maxDaysLabel.setStyle("-fx-font-size: 14px; -fx-text-fill: #666666;");

        // 打卡按钮
        Button checkInButton = new Button("打卡");
        checkInButton.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-font-size: 14px;");
        checkInButton.setPrefWidth(80);
        checkInButton.setOnAction(e -> {
            habit.checkIn();
            totalDaysLabel.setText("累计打卡天数：" + habit.getTotalCheckInDays());
            maxDaysLabel.setText("最长连续打卡天数：" + habit.getMaxContinuousDays());
        });

        card.getChildren().addAll(nameLabel, createDateLabel, totalDaysLabel, maxDaysLabel, checkInButton);
        return card;
    }

    private HBox createAddHabitBox() {
        HBox addBox = new HBox(10);
        addBox.setAlignment(Pos.CENTER_LEFT);
        addBox.setPadding(new Insets(10, 0, 0, 0));

        TextField habitInput = new TextField();
        habitInput.setPromptText("输入习惯名称");
        habitInput.setPrefWidth(200);

        Button addButton = new Button("添加习惯");
        addButton.setStyle("-fx-background-color: #2196F3; -fx-text-fill: white;");
        addButton.setPrefWidth(100);

        addButton.setOnAction(e -> {
            String name = habitInput.getText().trim();
            if (!name.isEmpty()) {
                Habit newHabit = new Habit(name);
                dataManager.addHabit(newHabit);
                refreshHabitsDisplay();
                habitInput.clear();
            }
        });

        addBox.getChildren().addAll(new Label("新建习惯:"), habitInput, addButton);
        return addBox;
    }

    private void refreshHabitsDisplay() {
        VBox newLayout = createMainLayout();
        mainLayout.getChildren().clear();
        mainLayout.getChildren().addAll(newLayout.getChildren());
    }
}